<?php
class ResidentsController extends AppController {

	var $name = 'Residents';
	var $helpers = array('Html', 'Form' );

	function index() {
		$this->Resident->recursive = 0;
		$this->set('residents', $this->Resident->findAll());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash('Invalid id for Resident.');
			$this->redirect('/residents/');
		}
		$this->set('resident', $this->Resident->read(null, $id));
	}

	function add() {
		if (empty($this->data)) {
			$this->render();
		} else {
			$this->cleanUpFields();
			if ($this->Resident->save($this->data)) {
				$this->Session->setFlash('The Resident has been saved');
				$this->redirect('/residents/');
			} else {
				$this->Session->setFlash('Please correct errors below.');
			}
		}
	}

	function addPictures() {
		if (empty($this->data)) {
			$this->render();
		} else {
			if (!empty($this->params['form']) && is_uploaded_file($this->params['form']['File']['tmp_name'])) {
				$fileName = $this->params['form']['File']['tmp_name'];
				$fileData = fread(fopen($fileName, "r"), $this->params['form']['File']['size']);
				if (strcmp(substr($filename, strlen($fileName) - 3), "zip")) {
					if ($this->Picture->savePictures($fileData)) {
						$this->Session->setFlash('Pictures uploaded');
						$this->redirect('residents/');
					} else {
						$this->Session->setFlash('Please correct errors below');
					}
				} else {
					$s = strrchr($fileName, '/');
					$name = substr($fileName, $s);
					if ($this->Picture->savePicture($name, $fileData)) {
						$this->Session->setFlash('Picture uploaded');
						$this->redirect('residents/');
					} else {
						$this->Session->setFlash('Please correct errors below');
					}
				}
			}
		}
	}

	function edit($id = null) {
		if (empty($this->data)) {
			if (!$id) {
				$this->Session->setFlash('Invalid id for Resident');
				$this->redirect('/residents/index');
			}
			$this->data = $this->Resident->read(null, $id);
		} else {
			$this->cleanUpFields();
			if ($this->Resident->save($this->data)) {
				$this->Session->setFlash('The Resident has been saved');
				$this->redirect('/residents/index');
			} else {
				$this->Session->setFlash('Please correct errors below.');
			}
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash('Invalid id for Resident');
			$this->redirect('/residents/index');
		}
		if ($this->Resident->del($id)) {
			$this->Session->setFlash('The Resident deleted: id '.$id.'');
			$this->redirect('/residents/index');
		}
	}


	function admin_index() {
		$this->Resident->recursive = 0;
		$this->set('residents', $this->Resident->findAll());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash('Invalid id for Resident.');
			$this->redirect('/admin/residents/index');
		}
		$this->set('resident', $this->Resident->read(null, $id));
	}

	function admin_add() {
		if (empty($this->data)) {
			$this->render();
		} else {
			$this->cleanUpFields();
			if ($this->Resident->save($this->data)) {
				$this->Session->setFlash('The Resident has been saved');
				$this->redirect('/admin/residents/index');
			} else {
				$this->Session->setFlash('Please correct errors below.');
			}
		}
	}

	function admin_edit($id = null) {
		if (empty($this->data)) {
			if (!$id) {
				$this->Session->setFlash('Invalid id for Resident');
				$this->redirect('/admin/residents/index');
			}
			$this->data = $this->Resident->read(null, $id);
		} else {
			$this->cleanUpFields();
			if ($this->Resident->save($this->data)) {
				$this->Session->setFlash('The Resident has been saved');
				$this->redirect('/admin/residents/index');
			} else {
				$this->Session->setFlash('Please correct errors below.');
			}
		}
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash('Invalid id for Resident');
			$this->redirect('/admin/residents/index');
		}
		if ($this->Resident->del($id)) {
			$this->Session->setFlash('The Resident deleted: id '.$id.'');
			$this->redirect('/admin/residents/index');
		}
	}

}
?>
